package cn.dnui_cr503.dnuifood_16110100503.fragment;


import android.os.Bundle;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.List;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.adapter.ShopAdapter;
import cn.dnui_cr503.dnuifood_16110100503.bean.Shop;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.ShopModel;

/**
 * A simple {@link Fragment} subclass.
 */

public class HomeFragment extends BaseFragment implements OnRetrofitListener<List<Shop>> {
    private RecyclerView recyclerView;
    private ShopAdapter adapter;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView=view.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        initViews();
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable  Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);

    }

    void initViews() {
        recyclerView= (RecyclerView)recyclerView.findViewById(R.id.shop_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    void initEvents() {

    }

    void initData() {
        ShopModel shopModel=new ShopModel();
        shopModel.getAllShops(this);
    }

    @Override
    public void onSuccess(List<Shop> object, int flag) {
        adapter=new ShopAdapter(getActivity(),object);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
    }
}
